package com.studentmanager;

import com.studentmanager.controller.CourseController;
import com.studentmanager.domain.Course;
import com.studentmanager.service.CourseService;
import com.studentmanager.util.PageBean;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.Collections;
import java.util.Map;

import static javafx.beans.binding.Bindings.when;
import static org.hamcrest.CoreMatchers.any;

public class coursetest {


        @Autowired
        private MockMvc mockMvc;

        @Mock
        private CourseService courseService;

        @InjectMocks
        private CourseController courseController;

        @BeforeEach
        void setUp() {
            mockMvc = MockMvcBuilders.standaloneSetup(courseController).build();
        }

        @Test
        void testGetClazzList() throws Exception {
            // Mocking the service method
            PageBean<Course> mockPageBean = new PageBean<>();
            mockPageBean.setDatas(Collections.singletonList(new Course()));
            mockPageBean.setTotalsize(1);

            when(courseService.queryPage(any(Map.class))).thenReturn(mockPageBean);

            // Call the controller and assert response
            mockMvc.perform(MockMvcRequestBuilders.post("/course/getCourseList")
                            .param("page", "1")
                            .param("rows", "100")
                            .param("from", "combox"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.size()").value(1));
        }

        @Test
        void testAddCourse() throws Exception {
            // Mocking the service method
            Course course = new Course();
            when(courseService.addCourse(any(Course.class))).thenReturn(1);

            // Call the controller and assert response
            mockMvc.perform(MockMvcRequestBuilders.post("/course/addCourse")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content("{\"name\":\"Math\"}"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("添加成功"));
        }

        @Test
        void testEditCourse() throws Exception {
            // Mocking the service method
            Course course = new Course();
            when(courseService.editCourse(any(Course.class))).thenReturn(1);

            // Call the controller and assert response
            mockMvc.perform(MockMvcRequestBuilders.post("/course/editCourse")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content("{\"id\":1, \"name\":\"Math Updated\"}"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("修改成功"));
        }

        @Test
        void testDeleteCourse() throws Exception {
            // Mocking the service method
            when(courseService.deleteCourse(any())).thenReturn(1);

            // Call the controller and assert response
            mockMvc.perform(MockMvcRequestBuilders.post("/course/deleteCourse")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content("{\"ids\":\"1,2\"}"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("删除成功"));
        }

        @Test
        void testDeleteCourseWithException() throws Exception {
            // Mocking the service method to throw an exception
            when(courseService.deleteCourse(any())).thenThrow(new RuntimeException("删除失败,该班级存在老师或学生"));

            // Call the controller and assert response
            mockMvc.perform(MockMvcRequestBuilders.post("/course/deleteCourse")
                            .contentType(MediaType.APPLICATION_JSON)
                            .content("{\"ids\":\"1,2\"}"))
                    .andExpect(MockMvcResultMatchers.status().isOk())
                    .andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false))
                    .andExpect(MockMvcResultMatchers.jsonPath("$.message").value("删除失败,该班级存在老师或学生"));
        }
    }